<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="招聘名称" prop="careerTitle">
        <el-input v-model="queryParams.careerTitle" placeholder="请输入招聘名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item v-if="!isCompany" label="主办单位" prop="hostOrg">
        <el-input v-model="queryParams.hostOrg" placeholder="请输入主办单位" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="召开时间" prop="meetTime">
        <el-date-picker clearable v-model="queryParams.meetTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择召开时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker clearable v-model="queryParams.endTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择结束时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="举办状态" prop="progState" v-if="isCompany">
        <el-select placeholder="请选择举办状态" v-model="queryParams.progState">
          <el-option v-for="item in holdStatusList" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="参会状态" prop="isEnroll" v-if="isCompany">
        <el-select placeholder="请选择参会状态" v-model="queryParams.isEnroll">
          <el-option v-for="item in joinStatusList" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" v-if="!isCompany">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:careerFair:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:careerFair:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:careerFair:export']">导出</el-button>
      </el-col>
      <div>共计有: {{ careerData.careerNum }}场招聘会, {{ careerData.comNum || 0 }}个单位, {{ careerData.stationNum || 0 }}个岗位, {{
        careerData.pageviews || 0 }}人浏览</div>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <!-- 企业端 -->
    <el-table v-loading="loading" v-if="isCompany" :data="careerFairList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="招聘会名称" align="center" prop="careerTitle" />
      <el-table-column label="举办时间" align="center" prop="hostAddr" width="260">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.meetTime, '{y}年{m}月{d}日') }} - {{ parseTime(scope.row.endTime, '{y}年{m}月{d}日')
            }}</span>
        </template>
      </el-table-column>
      <el-table-column label="组织单位" align="center" prop="hostOrg" />
      <el-table-column label="浏览量" align="center" prop="">
        <template slot-scope="scope">
          <span>{{ scope.row.pageviews }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收到简历" align="center" prop="handResume" />
      <el-table-column label="举办状态" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.progState === 0">未开始</span>
          <span v-if="scope.row.progState === 1">已开始</span>
          <span v-if="scope.row.progState === 2">已结束</span>
        </template>
      </el-table-column>
      <el-table-column label="参会状态" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.isEnroll === 0">未报名</span>
          <span v-if="scope.row.isEnroll === 1">已报名</span>
          <span v-if="scope.row.isEnroll === 2">已参会</span>
          <span v-if="scope.row.isEnroll === 3">报名不通过</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" v-hasPermi="['system:careerFair:detail']"
            @click="handleUpdate(scope.row, 'detail')">招聘会详情</el-button>
          <el-button v-if="scope.row.isEnroll === 2" size="mini" type="text" v-hasPermi="['system:careerFair:meeting']"
            @click="handleApply(scope.row, 'detail')">参会详情</el-button>
          <el-button v-if="scope.row.progState !== 2 && (scope.row.isEnroll === 0 || scope.row.isEnroll === 3)" size="mini" type="text" v-hasPermi="['system:careerFair:apply']"
            @click="handleApply(scope.row, 'apply')">报名</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 管理端 -->
    <el-table v-loading="loading" v-else :data="careerFairList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="招聘会名称" align="center" prop="careerTitle" />
      <el-table-column label="主办单位" align="center" prop="hostOrg" />
      <el-table-column label="举办时间" align="center" prop="hostAddr" width="260">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.meetTime, '{y}年{m}月{d}日') }} - {{ parseTime(scope.row.endTime, '{y}年{m}月{d}日')
            }}</span>
        </template>
      </el-table-column>

      <el-table-column label="参与单位" align="center" prop="underOrg" />
      <el-table-column label="需求岗位" align="center" prop="stationNumber" />
      <el-table-column label="学历统计" align="center" prop="eduNumber" width="140">
        <template slot-scope="scope" v-if="scope.row.eduNumber">
          <div v-for="item in splitStr(scope.row.eduNumber)">{{ item }}</div>
        </template>
      </el-table-column>
      <el-table-column label="区域统计" align="center" prop="compAreaNum" width="180">
        <template slot-scope="scope" v-if="scope.row.compAreaNum">
          <div v-for="item in splitStr1(scope.row.compAreaNum)">{{ item }}</div>
        </template>
      </el-table-column>
      <el-table-column label="浏览用户" align="center" prop="pageviews" />
      <el-table-column label="投递简历" align="center" prop="handResume" />
      <el-table-column label="关注人数" align="center" prop="followNum" />
      <el-table-column label="联系人数" align="center" prop="relateNum" />
      <el-table-column label="上下架" align="center" prop="">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.isShelveBool" @change="handleChangeStatus(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="企业管理" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleEnter(scope.row)"
            v-hasPermi="['system:careerFair:edit']">进入</el-button>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:careerFair:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:careerFair:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改招聘会管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="招聘标题" prop="careerTitle">
          <el-input v-model="form.careerTitle" placeholder="请输入招聘标题" />
        </el-form-item>
        <el-form-item label="宣传图" prop="hallImages">
          <image-upload :limit="1" resolution="358*160像素" v-model="form.hallImages" />
          <!-- <el-input v-model="form.hallImages" placeholder="请输入招聘标题" /> -->
        </el-form-item>
        <el-form-item label="主办地址" prop="hostAddr">
          <el-input v-model="form.hostAddr" placeholder="请输入主办地址" />
        </el-form-item>
        <el-form-item label="经纬度" class="is-required" prop="longitudeAndLatitude">
          <el-input v-model.trim="form.longitudeAndLatitude" placeholder="请输入经纬度" />
          <el-button class="mt10" type="primary" @click="openDialog = true">选择经纬度</el-button>
        </el-form-item>
        <el-form-item label="召开时间" prop="meetTime">
          <el-date-picker clearable v-model="form.meetTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择召开时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable v-model="form.endTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="主办单位" prop="hostOrg">
          <el-input v-model="form.hostOrg" placeholder="请输入主办单位" />
        </el-form-item>
        <el-form-item label="承办单位" prop="underOrg">
          <el-input v-model="form.underOrg" placeholder="请输入承办单位" />
        </el-form-item>
        <el-form-item label="协办单位" prop="jointlyOrg">
          <el-input v-model="form.jointlyOrg" placeholder="请输入协办单位" />
        </el-form-item>
        <el-form-item label="联系人" prop="liaison">
          <el-input v-model="form.liaison" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model="form.liaisonPhone" placeholder="请输入联系电话" />
        </el-form-item>

        <el-form-item label="活动详情">
          <editor v-model="form.eventDetail" :min-height="192" />
        </el-form-item>
        <el-form-item label="活动分布图">
          <editor v-model="form.eventDistrib" :min-height="192" />
        </el-form-item>
        <el-form-item label="参加单位">
          <el-table :data="companyList" ref="multipleTable" :reserve-selection="true" :row-key="getRowKey"
            style="width: 100%" max-height="250" @selection-change="handleSelectComp">
            <el-table-column type="selection" width="55" v-if="!isCompany">
            </el-table-column>
            <el-table-column prop="compName" label="单位名称" />
            <!-- <el-table-column v-if="isCompany" label="招聘岗位">
              <template slot-scope="scope">
              </template>
            </el-table-column>
            <el-table-column v-if="isCompany" label="招聘人数">
              <template slot-scope="scope">
              </template>
            </el-table-column> -->
          </el-table>
          <div v-if="selectCompList.length">
            <div><span style="color: #1890ff;">{{ selectCompList.length }}</span>家单位</div>
            <span v-for="item in selectCompList" :key="item.id" style="color: #1890ff;">{{ item.compName + '、' }}</span>
          </div>
          <!-- <el-button type="primary" @click="submitForm" style="margin-top: 4px;">添加</el-button> -->
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="!careerFormType" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 进入参与单位管理 -->
    <el-dialog :title="careerFairTitle" v-if="enterFlag" :visible.sync="enterFlag" width="70%" append-to-body>
      <joinCompany :careerId="careerId" :careerFairTitle="careerFairTitle"></joinCompany>
    </el-dialog>
    <!-- 企业端报名逻辑 -->
    <el-dialog :title="careerFairTitle" v-if="applyFlag" :visible.sync="applyFlag" width="70%" append-to-body>
      <apply :applyType="applyType" :applyData="applyData" @cancel="handleCancel"></apply>
    </el-dialog>
    <!-- 经纬度 -->
    <el-dialog title="获取经纬度" :visible.sync="openDialog" append-to-body width="80%">
      <iframe :src="'https://lbs.qq.com/getPoint/'" width="100%" height="750px" frameBorder="0"></iframe>
    </el-dialog>
  </div>
</template>

<script>
import { validatePhone, validateLandLine } from '@/utils/validate'
import { listCompany } from "@/api/system/company";
import { listCareerFair, getCareerFair, delCareerFair, addCareerFair, updateCareerFair, getCareerCount } from "@/api/system/careerFair";
import joinCompany from './joinCompany.vue'
import apply from "./apply.vue";
const holdStatusList = [{
  label: '未开始',
  value: 0
}, {
  label: '已开始',
  value: 1
}, {
  label: '已结束',
  value: 2
}]
// 
const joinStatusList = [{
  label: '未报名',
  value: 0
}, {
  label: '已报名',
  value: 1
}, {
  label: '已参会',
  value: 2
}, {
  label: '报名不通过',
  value: 3
}]
export default {
  name: "CareerFair",
  components: {
    apply,
    joinCompany
  },
  data() {
    const validatePhoneNum = (rule, value, callback) => {
      if (!validatePhone(value) && !validateLandLine(value)) {
        callback(new Error('联系方式格式不正确'))
      }
      callback()
    }
    return {
      // 遮罩层
      holdStatusList,
      joinStatusList,
      loading: true,
      enterFlag: false,
      openDialog: false,
      applyFlag: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 招聘会管理表格数据
      careerFairList: [],
      companyList: [],
      // 弹出层标题
      title: "",
      careerFairTitle: "",
      careerId: '',
      applyType: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        careerTitle: null,
        hostAddr: null,
        meetTime: null,
        endTime: null,
        hostOrg: null,
        underOrg: null,
        jointlyOrg: null,
        isEnroll: null,
        progState: null,
      },
      selectCompList: [],
      // 表单参数
      form: {},
      applyData: {},
      careerFormType: '',
      careerData: {},
      // 表单校验
      rules: {
        careerTitle: [
          { required: true, message: "招聘标题不能为空", trigger: "blur" }
        ],
        hallImages: [
          { required: true, message: "宣传图不能为空", trigger: "blur" }
        ],
        hostAddr: [
          { required: true, message: "主办地址不能为空", trigger: "blur" }
        ],
        meetTime: [
          { required: true, message: "召开时间不能为空", trigger: "blur" }
        ],
        endTime: [
          { required: true, message: "结束时间不能为空", trigger: "blur" }
        ],
        hostOrg: [
          { required: true, message: "主办单位不能为空", trigger: "blur" }
        ],
        liaison: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        // liaisonPhone: [
        //   { required: true, message: "联系电话不能为空", trigger: [ "blur", "change"] },
        //   { validator: validatePhoneNum, trigger:  [ "blur", "change"] }
        // ],
        eventDetail: [
          { required: true, message: "活动详情不能为空", trigger: "blur" }
        ],
        longitudeAndLatitude: [
          { required: true, message: "单位经纬度不能为空", trigger: "blur" }
        ],
      }
    };
  },
  computed: {
    isCompany() {
      return this.$store.state.user.roles[0] === 'company' && !!this.$store.state.user.userData.compId
    },
    companyId() {
      return this.$store.state.user.userData.compId
    }
  },
  created() {
    this.getList();
    this.getCompList();
    this.getCareerCountData();
  },
  methods: {
    /** 查询招聘会管理列表 */
    getList() {
      this.loading = true;
      listCareerFair(this.queryParams).then(response => {
        response.rows.forEach(item => {
          item.isShelveBool = item.isShelve === 0
        })
        this.careerFairList = response.rows;
        console.log(this.careerFairList, 'this.careerFairList')
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询人才招聘公司信息列表 */
    getCompList() {
      listCompany({
        pageNum: 1,
        pageSize: 999
      }).then(response => {
        this.companyList = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        careerTitle: null,
        hallImages: null,
        hostAddr: null,
        meetTime: null,
        endTime: null,
        hostOrg: null,
        underOrg: null,
        jointlyOrg: null,
        liaison: null,
        liaisonPhone: null,
        eventDetail: null,
        eventDistrib: null,
        companyInfos: null,
        longitudeAndLatitude: null
      };
      this.resetForm("form");
    },
    handleCancel() {
      this.applyFlag = false;
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.getCareerCountData()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // 公司表单多选中数据
    handleSelectComp(selection) {
      this.selectCompList = selection
      console.log(this.selectCompList, 'selection')
    },
    getRowKey(row) {
      console.log(row, 'rowww')
      return row.id;
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加招聘会管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row, type) {
      this.reset();
      const id = row.id || this.ids
      this.careerFormType = type
      getCareerFair(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = type === 'detail' ? '招聘会详情' : '修改招聘会管理';
        if (response.data.latitude && response.data.longitude) {
          const tempStr = response.data.latitude + ',' + response.data.longitude
          this.$set(this.form, 'longitudeAndLatitude', tempStr)
        }
        if (!type) {
          this.toggleSelection(response.data.companyInfos)
        } else {
          this.companyList = response.data.companyInfos
        }
      });
    },
    // 报名
    handleApply(row, type) {
      this.applyFlag = true
      this.applyData = row
      this.applyType = type
      this.careerFairTitle = type === 'apply' ? '招聘会报名申请' : '参会详情'
    },
    // 上下架
    handleChangeStatus(item) {
      item.isShelve = item.isShelveBool ? 0 : 1
      updateCareerFair(item).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.getList();
        this.getCareerCountData()
      });
    },
    // 进入
    handleEnter(item) {
      this.careerId = item.id
      this.enterFlag = true
      this.careerFairTitle = item.careerTitle
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.companyInfos = this.selectCompList
          this.form.latitude = this.form.longitudeAndLatitude.split(',')[0]
          this.form.longitude = this.form.longitudeAndLatitude.split(',')[1]
          if (this.form.id != null) {
            updateCareerFair(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.getCareerCountData()
            });
          } else {
            addCareerFair(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.getCareerCountData()
            });
          }
        }
      });
    },
    // 统计数据
    getCareerCountData() {
      getCareerCount().then(res => {
        this.careerData = res.data
      })
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除招聘会管理编号为"' + ids + '"的数据项？').then(function () {
        return delCareerFair(ids);
      }).then(() => {
        this.getList();
        this.getCareerCountData()
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/careerFair/export', {
        ...this.queryParams
      }, `careerFair_${new Date().getTime()}.xlsx`)
    },
    // 选中表格
    toggleSelection(rows) {
      this.companyList.forEach(row => {
        rows.forEach(item => {
          if (row.id == item.id) {
            this.$nextTick(() => {
              console.log(row, 'toggleSelection9999')
              this.$refs.multipleTable.toggleRowSelection(row, true);
            })
          }
        })
      });
    },
    splitStr(str) {
      let tempArr = []
      if (str && str.indexOf(',') > -1) {
        tempArr = str.split(',')
      } else {
        tempArr = [str]
      }
      return tempArr
    },
    splitStr1(str) {
      let tempArr = []
      if (str && str.indexOf('、') > -1) {
        tempArr = str.split('、')
      } else {
        tempArr = [str]
      }
      return tempArr
    }
  }
};
</script>
